package com.example.computerstroe.controller;

import com.example.computerstroe.service.ex.InsertException;
import com.example.computerstroe.service.ex.ServiceException;
import com.example.computerstroe.service.ex.UserNameDuplicationException;
import com.example.computerstroe.util.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;

public class BaseController {
    public static final Integer ok=200;
    @ExceptionHandler(ServiceException.class)
    public Result<Void> handleException(Throwable e){
        Result<Void>result=new Result<>(e);
        if(e instanceof UserNameDuplicationException){
            result.setStatus(4000);
            result.setMessage("用户名被占用");
        }else if(e instanceof InsertException){
            result.setStatus(5000);
            result.setMessage("增加用户时发送未知错误");
        }
        return result;

    }
}
